package com.caiyi.financial.nirvana.sms.controller;

import com.alibaba.fastjson.JSONObject;
import com.caiyi.financial.nirvana.sms.entity.SmsPermission;
import com.caiyi.financial.nirvana.sms.enums.ReturnCodeEnum;
import com.caiyi.financial.nirvana.sms.querybean.MenuBean;
import com.caiyi.financial.nirvana.sms.querybean.PageBean;
import com.caiyi.financial.nirvana.sms.querybean.PermissionQueryBean;
import com.caiyi.financial.nirvana.sms.service.SmsPermissionService;
import com.youyu.common.api.Result;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mario on 2018/1/8 0008.
 * 权限管理相关
 */
@Api(description = "权限管理接口")
@RestController
@RequestMapping("/sms/admin/permission")
public class PermissionController {

    @Autowired
    private SmsPermissionService smsPermissionService;

    @ApiOperation("查询权限")
    @PostMapping("/getPermissions")
    public Result getPermissions(@RequestBody PermissionQueryBean bean) {
        PageHelper.startPage(bean.getPageNum(), bean.getPageSize());
        Example example = new Example(SmsPermission.class);
        example.createCriteria()
                .andCondition("name like '%" + (bean.getName() == null ? "" : bean.getName()) + "%'")
                .andBetween("createTime", bean.getStartTime(), bean.getEndTime());
        example.setOrderByClause("code asc");
        Result result = new Result();
        result.setData(new PageBean<>(smsPermissionService.queryListByExample(example)));
        return result;
    }

    @ApiOperation("查询权限树")
    @PostMapping("/getPermissionsTree")
    public Result getPermissionsTree() {
        //将权限整合为菜单树
        List<MenuBean> menuBeanList = this.smsPermissionService.formatMenus(smsPermissionService.queryAll());
        Result result = new Result();
        result.setData(menuBeanList);
        return result;
    }

    @ApiOperation(value = "查询菜单DIC", notes = "此接口只返回的MENU，父节点下拉列表")
    @PostMapping("/getMenuDic")
    public Result getMenuDic() {
        Example example = new Example(SmsPermission.class);
        example.createCriteria().andEqualTo("type", "MENU");
        example.setOrderByClause("code asc");
        List<SmsPermission> menus = this.smsPermissionService.queryListByExample(example);
        List<JSONObject> dic = new ArrayList<>();
        JSONObject root = new JSONObject();
        root.put("code", "ROOT");
        root.put("name", "根节点");
        dic.add(root);
        for (SmsPermission item : menus) {
            JSONObject object = new JSONObject();
            object.put("code", item.getCode());
            object.put("name", item.getName());
            dic.add(object);
        }
        Result result = new Result();
        result.setData(dic);
        return result;
    }

    @ApiOperation("添加权限")
    @PostMapping("/addPermission")
    public Result addPermission(@RequestBody SmsPermission bean) {
        Result result = new Result();
        /*根据父节点CODE，自动补全CODE*/
        bean.setCode(this.smsPermissionService.getNextCode(bean.getParent()));

        /*插入前CODE查重*/
        Example example = new Example(SmsPermission.class);
        example.createCriteria().andEqualTo("code", bean.getCode());
        List<SmsPermission> checkList = smsPermissionService.queryListByExample(example);
        if (checkList.size() > 0) {
            result.setCode(ReturnCodeEnum.FAILED.getCode());
            result.setDesc("CODE已经存在");
            return result;
        }
        if (smsPermissionService.insertByEntitySelective(bean) != 1) {
            result.setDesc(ReturnCodeEnum.FAILED.getDesc());
            result.setCode(ReturnCodeEnum.FAILED.getCode());
            return result;
        } else {
            return new Result();
        }
    }

    @ApiOperation("删除权限")
    @PostMapping("/delPermission")
    public Result delPermission(@RequestParam Integer id) {
        SmsPermission smsPermission = new SmsPermission();
        smsPermission.setId(id);
        int res = smsPermissionService.deleteByPrimaryKey(smsPermission);
        if (res != 1) {
            Result result = new Result();
            result.setDesc(ReturnCodeEnum.FAILED.getDesc());
            result.setCode(ReturnCodeEnum.FAILED.getCode());
            return result;
        } else {
            return new Result();
        }
    }

    @ApiOperation("修改权限")
    @PostMapping("/modPermission")
    public Result modPermission(@RequestBody SmsPermission bean) {
        int res = smsPermissionService.updateByEntitySelective(bean);
        Result err = new Result();
        err.setDesc(ReturnCodeEnum.FAILED.getDesc());
        err.setCode(ReturnCodeEnum.FAILED.getCode());
        return res == 1 ? new Result() : err;
    }
}
